/*
 * @Date: 2023-08-08 15:34:34
 * @LastEditors: lyx
 * @LastEditTime: 2023-08-09 09:58:11
 * @FilePath: \sup-mini-app\src\components\Weather\hooks\useWeather.tsx
 * @Description:
 */
import React, { useEffect, useState } from "react";
import Taro from "@tarojs/taro";

function useWeather(props: any) {
  const { dispatch, weather } = props;
  const [currentWeather, setCurrentWeather] = useState<ILocalWeather>(
    {} as ILocalWeather
  );
  const getWeatherByLocation = async ({ latitude, longitude }) => {
    const queryDto = {
      latitude,
      longitude,
    };

    dispatch({
      type: "weather/queryWeatherByLocation",
      payload: {
        ...queryDto,
      },
    });
  };
  useEffect(() => {
    Taro.getLocation({
      type: "gcj02", //返回可以用于 Taro.openLocation的经纬度
      success: async (res) => {
        if (weather?.adcode) {
          return;
        }
        console.log("getLocation", res);
        const latitude = res.latitude;
        const longitude = res.longitude;
        console.log("latitude", latitude);
        console.log("longitude", longitude);
        getWeatherByLocation({ latitude, longitude });
      },
      fail: (res) => {
        console.log(res, "res");
      },
    });
  }, []);
  return {
    currentWeather,
  };
}

export default useWeather;
